
define(function (require, exports, module) {
    WebPDF.TextAnnotHandler = function (readerApp,annotHandlerManager) {
        this.readerApp = readerApp;
        this.annotHandlerManager = annotHandlerManager;
    };
    
    WebPDF.TextAnnotHandler.prototype =
    {
    	getType: function () { return "Text" ;  },
    	canAnswer: function (pAnnot, bAllowSelectTextTool)
    	{
    		var tool = this.readerApp.getCurToolHandler();
    		if (tool != null)
    		{
    			var toolName = tool.getName();
    			if (toolName ===  WebPDF.Tools.TOOL_NAME_SELANNOT || toolName === WebPDF.Tools.TOOL_NAME_HAND ||
                    toolName === WebPDF.Tools.TOOL_NAME_TEXTANNOT){
    				return true;
    			}

                if (bAllowSelectTextTool != undefined && bAllowSelectTextTool) {
                    if (toolName === WebPDF.TOOL_NAME_SELECTTEXT) {
                        return true;
                    }
                }
    		}
    		return false;
    	},
        canMove: function (subType) {
            return true;
        },
        moveAnnot: function(pageView, selectedAnnot,
                            offsetX, offsetY ,newPosX, newPosY){
            var annotName = selectedAnnot.getAnnotName();
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotDivID = annotUIManager.getAnnotDivID(pageView, annotName);
            var jqAnnotDiv = $("#" + annotDivID);
            jqAnnotDiv.css({
                left: newPosX + "px",
                top: newPosY + "px"
            });
            var annotRectDivID = annotUIManager.getTextAnnotDivID(pageView, annotName);
            var jqAnnotRect = $("#" + annotRectDivID);
            jqAnnotRect.css({
                left: newPosX + "px",
                top: newPosY + "px"
            });
        },
        deleteAnnot: function(pageView, annot){
            var annotName = annot.getAnnotName();
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotDivID = annotUIManager.getAnnotDivID(pageView, annotName);
            $("#" + annotDivID).remove();
            var annotShowDivID = annotUIManager.getTextAnnotDivID(pageView, annotName);
            $("#" + annotShowDivID).remove();
            var popup = annot.getPopup();
            if (popup == null || annot.getHeadAnnot() != null)  {
                return ;
            }
            var popupDivID = annotUIManager.getAnnotPopupDivID(pageView, annotName);
            $("#" + popupDivID).remove();
        },
        onDraw: function(pageView, annot, isRedraw, extraScale){
        },
        onSelected: function(pageView, annot){
            var annotName = annot.getAnnotName();
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotDivID = annotUIManager.getAnnotDivID(pageView, annotName);
            $("#" + annotDivID).addClass("fwr-annot-selected");
            /*$(".fwr_annot_selected").jeegoocontext(_MenuID, {
             onSelect: function (e, context) { return _self.OnSelect($(this)); }
             });*/
            return true;
        },
        onDeSelected: function(pageView, annot){
            var annotName = annot.getAnnotName();
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotDivID = annotUIManager.getAnnotDivID(pageView, annotName);
            $("#" + annotDivID).removeClass("fwr-annot-selected");
        },
        onFocus: function(pageView, annot){
             this.annotHandlerManager.getMarkupAnnotHandler().onFocus(pageView, annot);
        },
    	onLButtonDown: function(pageView,annot,event){
    	   return this.onSelected(pageView,annot);
    	},
        onLButtonUp: function(pageView,annot, event){
        },
    	onLButtonDblClk: function(pageView,annot,event){
            this.annotHandlerManager.getMarkupAnnotHandler().onLButtonDblClk(pageView, annot,event);
    	},
        onMouseOver: function(pageView, annot, event){
            this.annotHandlerManager.getMarkupAnnotHandler().onMouseOver(pageView, annot,event);
        },
        onMouseLeave : function (pageView, annot, event) {
            this.annotHandlerManager.getMarkupAnnotHandler().onMouseLeave(pageView, annot,event);
        },
        onUpdatePosition: function(pageView, annot, visibleRect){
            var pdfPage = pageView.getPDFPage();
            var annotPDFRect = annot.getRect();
            var annotDocRect = pageView.pdfToDoc(annotPDFRect, true);
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotShowDivID = annotUIManager.getTextAnnotDivID(pageView, annot.getAnnotName());

            var annotDivID = annotUIManager.getAnnotDivID(pageView, annot.getAnnotName());
            var jqAnnotDiv = $("#" + annotDivID);
            jqAnnotDiv.css({
                left: annotDocRect.left,
                top: annotDocRect.top,
                width: WebPDF.RectUtils.width(annotDocRect),
                height: WebPDF.RectUtils.height(annotDocRect)
            });

            $("#" + annotShowDivID).css({
                left: annotDocRect.left,
                top: annotDocRect.top,
                width: WebPDF.RectUtils.width(annotDocRect),
                height: WebPDF.RectUtils.height(annotDocRect)
            });
        }
    };

    return WebPDF.TextAnnotHandler;
});